package com.atguigu.serviceedu.service.impl;

import com.atguigu.serviceedu.entity.EduChapter;
import com.atguigu.serviceedu.entity.EduComment;
import com.atguigu.serviceedu.entity.EduVideo;
import com.atguigu.serviceedu.entity.vo.ChapterVO;
import com.atguigu.serviceedu.entity.vo.SiteCourseVO;
import com.atguigu.serviceedu.entity.vo.VideoVO;
import com.atguigu.serviceedu.mapper.EduChapterMapper;
import com.atguigu.serviceedu.mapper.EduCourseMapper;
import com.atguigu.serviceedu.mapper.EduVideoMapper;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-06-18
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduChapterMapper eduChapterMapper;
    @Autowired
    private EduVideoMapper eduVideoMapper;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduCourseMapper eduCourseMapper;
    @Override
    public List<ChapterVO> queryNestByCourseId(String id) {
        QueryWrapper<EduChapter> eduChapterQueryWrapper = new QueryWrapper<>();
        eduChapterQueryWrapper.eq("course_id",id);
        eduChapterQueryWrapper.orderByAsc("sort","id");
        List<EduChapter> chapterList = eduChapterMapper.selectList(eduChapterQueryWrapper);//拿到这个courseid下的所有chapter

        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id",id);
        List<EduVideo> videoList = eduVideoMapper.selectList(eduVideoQueryWrapper);//拿到这个courseid下的所有video

        List<ChapterVO>chapterVOList=new ArrayList<>();//这个局部变量是一个装chapterVO的大袋子，用于返回给前端，每个chapterVO里面又会把
        //这个chapter下的所有videoVO都装完
        for (int i=0;i<chapterList.size();i++){
            EduChapter eduChapter = chapterList.get(i);//从chapter集合里面取出一个chapter
            ChapterVO chapterVO = new ChapterVO();//造一个前端需要的数据结构chapterVO对象（它只包含了chapter中前端需要的属性）
            BeanUtils.copyProperties(eduChapter,chapterVO);//给chapterVO里的属性赋值，当然就用的是比它全面的chapter
            String chapterId=eduChapter.getId();//拿到chapter的的id,原因是我要把该chapter下的video都装进去，要依赖id作为判断条件

            for (int j=0;j<videoList.size();j++){
                EduVideo eduVideo = videoList.get(j);//从video集合中取一个video
                String eduVideoChapterId = eduVideo.getChapterId();
                if (eduVideoChapterId.equals(chapterId)){//如果这个video的字段chapterid和大循环正在循环chapter的id相等
                    VideoVO videoVO = new VideoVO();//就先造出video的前端缩小版videovo
                    BeanUtils.copyProperties(eduVideo,videoVO);//就将video的属性赋值给它的前端缩小版videoVO
                    chapterVO.getChildren().add(videoVO);//这里会循环的找出所有该chapter下的video,组装出它对应的前端缩小版videoVO,并全都装到chaptervo中去
                }
            }
            chapterVOList.add(chapterVO);
        }
        return chapterVOList;
    }

    @Override
    public boolean deleteChapterById(String id) {
        //首先第一步要删阿里云
         eduVideoService.removeOnAliyunVideoByChapterId(id);
        //再删该chapter下的所有video
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("chapter_id",id);
        int delete = eduVideoMapper.delete(eduVideoQueryWrapper);
        //再删该chapter
        return this.removeById(id);
    }

    @Override
    public int  saveChapterById(EduChapter eduChapter) {
        int i = eduChapterMapper.updateById(eduChapter);
        return i;
    }
}
